package d8_stream;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 2024/6/7 Cjr Stream常见的流终结方法
 */
public class StreamTest4 {
    public static void main(String[] args) {
        List<Student> students = new ArrayList<>();
        Student s1= new Student("蜘蛛精",23,176.3);
        Student s2= new  Student("紫霞",26,187.4);
        Student s3 = new Student("紫霞", 15, 156.4);
        Student s4 = new Student("至尊宝", 22, 178.7);
        Collections.addAll(students,s1,s2,s3,s4);

        //1.请计算出身高超过168的学生有几人
        long count = students.stream().filter(s -> s.getHeight() > 168).count();
        System.out.println(count);

        //2.请找出身高最高的学生对象,并输出
        Student s=  students.stream().max((o1, o2) -> Double.compare(o1.getHeight(), o2.getHeight())).get();
        System.out.println(s);

        //3.请找出身高最矮的学生对象,并输出
        Student stu=  students.stream().min((o1,o2)->Double.compare(o1.getHeight(),o2.getHeight())).get();
        System.out.println(stu);

        //4.请找出身高超过170的学生对象, 并放到一个新集合中去返回

        //流只能收集一次
        List<Student> collect= students
                                        .stream()
                                        .filter(st -> st.getHeight() > 170)
                                        .collect(Collectors.toList());
                                        System.out.println(collect);
/*
        Set<Student> collect1 = students
                                        .stream()
                                        .filter(st -> st.getHeight() > 170)
                                        .collect(Collectors.toSet());*/
        System.out.println(collect);

        //5.请找出身高超过170的学生对象, 并把学生对象的名字和身高,存入到一个Map集合返回
        Map<String, Double> map = students
                .stream()
                .filter(f -> f.getHeight() > 170)
                .collect(Collectors.toMap(a -> a.getName(), a -> a.getHeight()));
        System.out.println(map);

        //6.收集到学生数组中
        Student[] array = students.stream().filter(f -> f.getHeight() > 170)
                .toArray(len -> new Student[len]);
        System.out.println(Arrays.toString(array));

        System.out.println("-------");
    }
}
